package com.dotflat.fin.domain;

import com.dotflat.fin.dto.AppPayResult;
import com.dotflat.rest.domain.AreableObjectBase;
import com.dotflat.rest.utils.ReadonlyField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import java.util.Date;

/**
 * 支付记录
 */

@Data
@Entity
@Table(name="fin_pay_record")
@TypeDef(name = "json", typeClass = JsonStringType.class)
@EqualsAndHashCode(callSuper = true)
public class PayRecord extends AreableObjectBase  {

    @ReadonlyField
    @Column(columnDefinition = "INT default 0 COMMENT '总金额'")
    private Long totalFee = 0L;

    @Column(columnDefinition = "varchar(32) COMMENT '账单id'")   //可能存在多个record对象对应一个order对象
    private String payOrderId;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "payOrderId", columnDefinition = "varchar(32) COMMENT '账单id'", insertable = false, updatable = false)
//    @JsonIgnore
    @ApiModelProperty(hidden = true)
    private FinPlanOrder finPlanOrder;

    @Column(columnDefinition = "varchar(32) COMMENT '收费账号'")
    private String payAccountId;

    @Column(name = "trade_type")
    private String tradeType;

    @JsonIgnore
    @Column(columnDefinition = "varchar(64) COMMENT '银行流水号'")
    private String serialNumber;

    @JsonIgnore
    @Column(columnDefinition="DATETIME COMMENT '结算时间'")
    private Date settlementTime;

    @Column(columnDefinition = "INT default 0 COMMENT '需要扣减的定金'")
    private Long useDeposit = 0L;

    @JsonIgnore
    @Column(columnDefinition = "INT default 0 COMMENT '已支付金额'")
    private Long paiedMoney = 0L;

    @Column(columnDefinition = "VARCHAR(16) COMMENT '实付类型:alipay, wechat'")
    private String payType;

    @Column(columnDefinition = "INT DEFAULT 0 COMMENT '支付状态1:未付款;2:已付款,3:支付中'")
    private Integer payStatus;

    @Column(columnDefinition = "VARCHAR(32) COMMENT '支付流水号'")
    private String payId;

    @Column(columnDefinition = "VARCHAR(32) COMMENT '支付成功时间'")
    private Date payTime;

    private String attach;

    @Type(type = "json")
    @Column(columnDefinition = "JSON COMMENT '支付结果' ")
    private AppPayResult payResult;

    @Transient
    private String className;

    public static PayRecord of(FinPlanOrder finPlanOrder){
        PayRecord payRecord = new PayRecord();

        payRecord.setCreatedBy(finPlanOrder.getCreatedBy());
        payRecord.setPayOrderId(finPlanOrder.getId());

        return payRecord;
    }
}